package com.kymatrix.mpo.plus.bpm.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.kymatrix.mpo.plus.bpm.server.dao.definition.BpmFormMapper;
import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmFormEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.PageQueryBpmFormReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.SaveBpmFormReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.form.UpdateBpmFormReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmFormService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.exception.BusinessException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * 动态表单 Service 实现类
 *
 * @author 风里雾里
 */
@Service
@Validated
public class BpmFormServiceImpl implements BpmFormService {

    @Resource
    private BpmFormMapper formMapper;

    @Override
    public String createForm(SaveBpmFormReq createReqVO) {
        BpmFormEntity bpmFormEntity = new BpmFormEntity();
        bpmFormEntity.setName(createReqVO.getName());
        bpmFormEntity.setStatus(createReqVO.getStatus());
        bpmFormEntity.setConf(createReqVO.getConf());
        bpmFormEntity.setFields(createReqVO.getFields());
        bpmFormEntity.setRemark(createReqVO.getRemark());
        
        formMapper.insert(bpmFormEntity);
        return bpmFormEntity.getId();
    }

    @Override
    public void updateForm(UpdateBpmFormReq updateReqVO) {
        BpmFormEntity bpmFormEntity = this.formMapper.selectById(updateReqVO.getId());
        if (bpmFormEntity == null) {
            throw new BusinessException("目标数据不存在" );
        }
        bpmFormEntity.setName(updateReqVO.getName());
        bpmFormEntity.setStatus(updateReqVO.getStatus());
        bpmFormEntity.setConf(updateReqVO.getConf());
        bpmFormEntity.setFields(updateReqVO.getFields());
        bpmFormEntity.setRemark(updateReqVO.getRemark());
        formMapper.updateById(bpmFormEntity);
    }

    @Override
    public void deleteForm(String id) {
        formMapper.deleteById(id);
    }

    @Override
    public BpmFormEntity getForm(String id) {
        return formMapper.selectById(id);
    }

    @Override
    public List<BpmFormEntity> getFormList() {
        return formMapper.selectList();
    }

    @Override
    public List<BpmFormEntity> getFormList(Collection<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return formMapper.selectBatchIds(ids);
    }

    @Override
    public PageResp<BpmFormEntity> getFormPage(PageQueryBpmFormReq pageReqVO) {
        return formMapper.selectPage(pageReqVO);
    }


}
